package biolabs.model.diffusion

import biolabs.core.UnaryAction
import biolabs.model.abscretion.SubstanceHolder
import biolabs.space.ContinuousPosition
import biolabs.space.DiscretePosition
import biolabs.space.Grid
import biolabs.space.Grids
import biolabs.util.TransformingCollection

import jsr166y.ForkJoinTask

import scala.collection.JavaConversions._

class Diffusion[
	P <: DiscretePosition[P, C],
	C <: ContinuousPosition[C, P]
](coefficient: Double) extends UnaryAction[SubstanceHolder[P, C], Unit, Unit] {

	override def perform(target: SubstanceHolder[P, C], notifyProgress: Unit=>Unit): Unit = {
		// perform the diffusion of each substance
		val diffuser = new Diffuser[P,C](target.getConcentrationMask, coefficient)
		val tr = new DiffusionTransformation[P,C](diffuser)
		val actions = new TransformingCollection(target.getSubstances map {target.getConcentration(_)}, tr)
		ForkJoinTask.invokeAll(actions)
	}

}